<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>学生成绩管理</title>
    <link rel="stylesheet" href="./layui/css/layui.css">
    <script src="./layui/layui.js"></script>
    <style>
        .layui-footer{position:static!important;width:auto;}
    </style>
</head>
<body>
<div class="layui-layout layui-layout-admin">
    <div class="layui-header">
        <div class="layui-logo layui-hide-xs layui-bg-black">教育工程认证系统</div>
        <ul class="layui-nav layui-layout-left">
            <li class="layui-nav-item layui-show-xs-inline-block layui-hide-sm" lay-header-event="menuLeft">
                <i class="layui-icon layui-icon-spread-left"></i>
            </li>
            <li class="layui-nav-item layui-hide-xs"><a href="">分支系统</a></li>
            <li class="layui-nav-item layui-hide-xs"><a href="">备份系统</a></li>
            <li class="layui-nav-item layui-hide-xs"><a href="">追溯系统</a></li>
            <li class="layui-nav-item">
                <a href="javascript:;">日志系统</a>
                <dl class="layui-nav-child">
                    <dd><a href="">日志服务1</a></dd>
                    <dd><a href="">日志服务2</a></dd>
                    <dd><a href="">日志服务3</a></dd>
                </dl>
            </li>
        </ul>
        <ul class="layui-nav layui-layout-right">
            <li class="layui-nav-item layui-hide layui-show-md-inline-block">
                <a href="javascript:;">
                    <img src="//tva1.sinaimg.cn/crop.0.0.118.118.180/5db11ff4gw1e77d3nqrv8j203b03cweg.jpg" class="layui-nav-img">
                    tester
                </a>
                <dl class="layui-nav-child">
                    <dd><a href="">个人信息</a></dd>
                    <dd><a href="">退出登录</a></dd>
                </dl>
            </li>
            <li class="layui-nav-item" lay-header-event="menuRight" lay-unselect>
                <a href="javascript:;"><i class="layui-icon layui-icon-more-vertical"></i></a>
            </li>
        </ul>
    </div>

    <div class="layui-side layui-bg-black">
        <div class="layui-side-scroll">
            <ul class="layui-nav layui-nav-tree" lay-filter="side-nav">
                <li class="layui-nav-item layui-nav-itemed">
                    <a class="" href="javascript:;">认证</a>
                    <dl class="layui-nav-child">
                        <dd><a href="Main.html">主页</a></dd>
                        <dd><a href="Student.html">学生信息管理</a></dd>
                        <dd><a href="Major.html">专业信息管理</a></dd>
                        <dd><a href="Teacher.html">教师信息管理</a></dd>
                        <dd><a href="Course.html">学科信息管理</a></dd>
                        <dd class="layui-this"><a href="GradeStudentTotal.html">学生成绩管理</a></dd>
                    </dl>
                </li>
                <li class="layui-nav-item">
                    <a href="javascript:;">漏洞扫描管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="ScanResult.html">扫描结果</a></dd>
                        <dd><a href="javascript:;">风险等级</a></dd>
                        <dd><a href="javascript:;">安全漏洞</a></dd>
                    </dl>
                </li>
            </ul>
        </div>
    </div>

    <div class="layui-body">
        <div style="padding: 15px;">
            <!-- 搜索框 -->
            <fieldset class="layui-elem-field">
                <legend>筛选条件</legend>
                <div class="layui-field-box">
                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label">学生ID</label>
                        <div class="layui-input-inline">
                            <input type="text" id="search_studentId" placeholder="请输入学生ID" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label">课程ID</label>
                        <div class="layui-input-inline">
                            <input type="text" id="search_courseId" placeholder="请输入课程ID" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label">学期</label>
                        <div class="layui-input-inline">
                            <input type="text" id="search_semester" placeholder="如 2024-2025-1" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-pane">
                        <div class="layui-inline">
                            <button class="layui-btn layui-btn-radius layui-btn-warm" id="iReset">重置</button>
                            <button class="layui-btn layui-btn-radius layui-btn-normal" id="searchVal">搜索</button>
                        </div>
                    </div>
                </div>
            </fieldset>

            <!-- 表格（前端分页） -->
            <div style="padding: 15px;">
                <table class="layui-hide" id="test" lay-filter="test"></table>
            </div>
        </div>
    </div>

    <div class="layui-footer">copyright © zero_cavalier 2025</div>
</div>

<!-- jQuery -->
<script src="js/jquery-3.6.0.js" charset="utf-8"></script>

<script>
    // 顶部事件（可选）
    layui.use(['element','layer','util'], function(){
        var layer = layui.layer, util = layui.util;
        util.event('lay-header-event', {
            menuLeft: function(){ layer.msg('展开左侧菜单的操作', {icon: 0}); },
            menuRight: function(){
                layer.open({
                    type: 1,
                    content: '<div style="padding: 15px;">处理右侧面板的操作</div>',
                    area: ['260px', '100%'],
                    offset: 'rt',
                    anim: 5,
                    shadeClose: true
                });
            }
        });
    });

    // 初始化表格：先渲染一个空表，启用前端分页
    layui.use('table', function () {
        const table = layui.table;
        table.render({
            elem: '#test',
            id: 'test',
            data: [],               // 初始空数据
            page: true,             // 前端分页：只要使用 data 而不是 url，即为前端分页
            limit: 10,              // 每页条数
            limits: [10,20,50,100],
            title: '学生成绩表',
            cols: [[
                {type:'numbers', title:'#', width:60},
                {field:'studentId',   title:'学生ID',   width:140, sort:true},
                {field:'courseId',    title:'课程ID',   width:140, sort:true},
                {field:'semester',    title:'学期',     width:140},
                {field:'regularScore',title:'平时成绩', width:120, templet:d=>fmt(d.regularScore)},
                {field:'labScore',    title:'实验成绩', width:120, templet:d=>fmt(d.labScore)},
                {field:'examScore',   title:'考试成绩', width:120, templet:d=>fmt(d.examScore)},
                {field:'finalScore',  title:'总评成绩', width:120, templet:d=>fmt(d.finalScore)}
            ]]
        });
    });

    // 点击重置
    $("#iReset").click(function () {
        $("#search_studentId").val("");
        $("#search_courseId").val("");
        $("#search_semester").val("");
        fetchAndFill(); // 不带条件查询全部
    });

    // 点击搜索
    $("#searchVal").click(function () {
        fetchAndFill();
    });

    // 统一的“拉数据并前端分页渲染”函数
    function fetchAndFill(){
        const studentId = $("#search_studentId").val().trim();
        const courseId  = $("#search_courseId").val().trim();
        const semester  = $("#search_semester").val().trim();

        // 约定：后端 /api/student-courses 接收这三参数做过滤，返回 {code:0, data:[...]}
        const params = {};
        if (studentId) params.studentId = studentId;
        if (courseId)  params.courseId  = courseId;
        if (semester)  params.semester  = semester;

        $.ajax({
            url: '/api/student-courses',
            type: 'GET',
            data: params,
            success: function(res){
                // 兼容：纯数组 或 {code:0,data:[...]}
                let rows = Array.isArray(res) ? res : (res.data || []);
                // 渲染到表格并启用前端分页
                layui.use('table', function () {
                    const table = layui.table;
                    table.reload('test', {
                        data: rows,              // 前端数据源
                        page: { curr: 1 }        // 回到第一页
                    });
                });
            },
            error: function(xhr){
                layer.msg('查询失败', {icon:2});
                console.error(xhr.responseText || xhr);
            }
        });
    }

    // 数字显示：null/空显示“-”，其余保留 1 位小数
    function fmt(v){
        if (v === null || v === undefined || v === '') return '-';
        const n = Number(v);
        return isNaN(n) ? '-' : n.toFixed(1);
    }

    // 首次加载（不带条件）
    fetchAndFill();
</script>
</body>
</html>
